package com.tide.crm.service.impl;

import com.tide.crm.dao.CegtMdmOrgAdjustRecordDao;
import com.tide.crm.dao.CustomerContactsDao;
import com.tide.crm.dao.CustomerInfoDao;
import com.tide.crm.dto.CustomerDto;
import com.tide.crm.dto.LoginUser;
import com.tide.crm.model.CustomerContacts;
import com.tide.crm.model.CustomerInfo;
import com.tide.crm.service.CustomerInfoService;
import com.tide.crm.utils.SnowFlakeIdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

    @Autowired
    private CustomerInfoDao customerInfoDao;

    @Autowired
    private CustomerContactsDao customerContactsDao;

    @Autowired
    private CegtMdmOrgAdjustRecordDao cegtMdmOrgAdjustRecordDao;

    @Override
    public String saveCustomer(Map<String,Object> params
            //CustomerDto customerDto, String[] postArray,String[] fullNameArray,String[] mobileArray,String[] telArray,String[] emailArray
            ,LoginUser user) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCorporateName(params.get("corporateName").toString());
        customerInfo.setCustomerType(params.get("customerType").toString());
        customerInfo.setCompanySize(params.get("companySize").toString());
        customerInfo.setMainIndustry(params.get("mainIndustry").toString());
        customerInfo.setMainBusiness(params.get("mainBusiness").toString());
        Date date = new Date();
//        BeanUtils.copyProperties(customerDto,customerInfo);
        customerInfo.setCreater(user.getUsername());
        customerInfo.setCreateDate(date);
        customerInfo.setModifyer(user.getUsername());
        customerInfo.setModifyDate(date);
        customerInfoDao.save(customerInfo);


        String[] postArray = (String[]) params.get("post");
        String[] fullNameArray = (String[]) params.get("fullName");
        String[] mobileArray = (String[]) params.get("mobile");
        String[] telArray = (String[]) params.get("tel");
        String[] emailArray = (String[]) params.get("email");
        int index = 0;
        if (postArray.length > 0){
            for (String post : postArray) {
                CustomerContacts contact = new CustomerContacts();
                contact.setCustomerId(customerInfo.getId());
                contact.setPostCode(post);
                if (fullNameArray.length > 0){
                    if (StringUtils.isNotBlank(fullNameArray[index])){
                        contact.setFullName(fullNameArray[index]);
                    }
                } 
                if (mobileArray.length > 0){
                    if (StringUtils.isNotBlank(mobileArray[index])){
                        contact.setComtactPhone(mobileArray[index]);
                    }
                }
                if (telArray.length > 0){
                    if (StringUtils.isNotBlank(telArray[index])){
                        contact.setComtactNumber(telArray[index]);
                    }
                }
                if (emailArray.length > 0){
                    if (StringUtils.isNotBlank(emailArray[index])){
                        contact.setEmail(emailArray[index]);
                    }
                }
                contact.setCreater(user.getUsername());
                contact.setCreateDate(date);
                contact.setModifyer(user.getUsername());
                contact.setModifyDate(date);
                customerContactsDao.save(contact);
                index++;
            }
        }
        return "success";
    }

    @Override
    public CustomerDto getById(Long id) {
        CustomerInfo customerInfo = customerInfoDao.getById(id);
        List<CustomerContacts> contactsList = customerContactsDao.findListByCusId(id);
        CustomerDto customerDto = new CustomerDto();
        BeanUtils.copyProperties(customerInfo,customerDto);
        customerDto.setContactsList(contactsList);
        return customerDto;
    }

    @Override
    public String updateCustomer(CustomerDto customerDto,String[] postArray,String[] fullNameArray,String[] mobileArray,String[] telArray,String[] emailArray, LoginUser user) {
        CustomerInfo customerInfo = new CustomerInfo();
        Date date = new Date();
        BeanUtils.copyProperties(customerDto,customerInfo);
        customerInfo.setModifyer(user.getUsername());
        customerInfo.setModifyDate(date);
        long customerId = customerInfoDao.update(customerInfo);
        int index = 0;
        customerContactsDao.deleteByCusId(customerDto.getId());
        if (postArray.length > 0){
            for (String post : postArray) {
                CustomerContacts contact = new CustomerContacts();
                contact.setCustomerId(customerDto.getId());
                contact.setPostCode(post);
                if (fullNameArray.length > 0){
                    if (StringUtils.isNotBlank(fullNameArray[index])){
                        contact.setFullName(fullNameArray[index]);
                    }
                }
                if (mobileArray.length > 0){
                    if (StringUtils.isNotBlank(mobileArray[index])){
                        contact.setComtactPhone(mobileArray[index]);
                    }
                }
                if (telArray.length > 0){
                    if (StringUtils.isNotBlank(telArray[index])){
                        contact.setComtactNumber(telArray[index]);
                    }
                }
                if (emailArray.length > 0){
                    if (StringUtils.isNotBlank(emailArray[index])){
                        contact.setEmail(emailArray[index]);
                    }
                }
                contact.setCreater(user.getUsername());
                contact.setCreateDate(date);
                contact.setModifyer(user.getUsername());
                contact.setModifyDate(date);
                customerContactsDao.save(contact);
               index++;
            }
        }
        return "success";
    }

    @Override
    public int count(Map<String, Object> params) {
        return customerInfoDao.count(params);
    }

    @Override
    public List<CustomerDto> list(Map<String, Object> params, Integer offset, Integer limit) {
        List<CustomerInfo> customerInfoList = customerInfoDao.list(params,offset,limit);
        SnowFlakeIdGenerator worker = new SnowFlakeIdGenerator(2);
        List<CustomerDto> customerDtoList = new ArrayList<CustomerDto>();
        if (!customerInfoList.isEmpty()){
            for (CustomerInfo customerInfo : customerInfoList) {
                List<CustomerContacts> contactsList = customerContactsDao.findListByCusId(customerInfo.getId());
                CustomerDto customerDto = new CustomerDto();
                BeanUtils.copyProperties(customerInfo,customerDto);
                customerDto.setContactsList(contactsList);
                customerDtoList.add(customerDto);
            }
        }
        return customerDtoList;
    }

    @Override
    public List<String> getEventNowInfo(String username) {
        return customerInfoDao.getEventNowInfo(username);
    }
}
